#!/usr/bin/perl -w

##HACKABOT_HELP##
# Do you feel lucky?
# !google search phrase
##HACKABOT_HELP##

use strict;
use URI::Escape;

my ($search, $asker);
while (<>) {
	if (/^nick\s*(\S*)/) {
		$asker = $1;
	}
	elsif (/^msg\s*(.*)/) {
		$search = $1;
	}
}

if (defined $search and defined $asker) {
	$search = uri_escape($search);
	my $google = `lynx --head --dump "http://www.google.com/search?q=$search\&btnI=I%27m+Feeling+Lucky"`;
	my $url;
	foreach(split(/\n/,$google)) {
		if (/^Location:\s*(.+)/) {
			$url = $1;
		}
	}
	if (defined $url) {
		print "send $asker: Google says $url\n";
	}
	else {
		print "send $asker: Google didn't say much :-/\n";
	}
}
else {
	die "see: All info not found :-/\n";
}
